#include <iostream>
 
using namespace std;
 
void permutation( int *m, int l, int n )
{
  if( l==n-1 ) 
  {
    for( int i=0; i<n; i++) 
	cout << m[i];
    cout << "\n ";
  }
  else 
  {    
    for( int i=l; i<n; i++ )
    {
      int tmp = m[l];
      m[l] = m[i];
      m[i] = tmp;    
      permutation( m, l+1, n );
      tmp = m[l];
      m[l] = m[i];
      m[i] = tmp;    
    }
  }
}
 
int main()
{
  int n = 5;
  int *m = new int[n];
 
  for( int i=0; i<n; i++)
    m[i] = i+1;
 
  permutation( m, 0, n );
  system("pause");
}